#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include "het_ringbuf.h" 
#include "het_chatgpt.h"  // <-- 唯一必须包含的头文件


#define FRAME_SIZE 160 // 假设16kHz采样率，每帧10ms
#define ENERGY_THRESHOLD 6000000.0
#define ZCR_THRESHOLD 40
#define SAMPLE_RATE 16000
#define SILENCE_DURATION 0.5 // 0.5秒

double calculate_energy(const int16_t *audio_data, size_t frame_size, int16_t channel_num);
double calculate_zcr(const int16_t *audio_data, size_t frame_size, int16_t channel_num);

static int in_speech = 0; // 是否在说话
static size_t speech_no = 0; // 是否在说话

static double speech_avg = 0;
static double speech_max = 0;
// 断句函数
int split_audio(const int16_t *audio_data, size_t audio_length , int16_t channel_num) {
    size_t start = 0;
    double energy = 0;
    double zcr = 0;
    size_t silence_frames = 0;
    size_t frames_size = FRAME_SIZE * channel_num ;
    size_t max_silence_frames = (size_t)(SILENCE_DURATION * SAMPLE_RATE);

    for (size_t i = 0; i <= audio_length - frames_size ; i += frames_size) {
        energy = calculate_energy(&audio_data[i], frames_size, channel_num);
        zcr = calculate_zcr(&audio_data[i], frames_size, channel_num);
        
        //HET_LOGI("calculate: index=[%d/%d]@%d\n", i,audio_length,frames_size/2,channel_num);
        speech_avg = (speech_avg * speech_no + energy) / (speech_no+1);
        speech_max = speech_max > energy?speech_max:energy;
        //printf("{\"pcm\": {\"start\": \"%zu\", \"energy\": \"%f\", \"zcr\": \"%f\", \"avg\": \"%f\", \"max\": \"%f\", \"speech\": \"%d\"}}\n", speech_no++*frames_size, energy, zcr, speech_avg, speech_max, in_speech);
        if (energy < ENERGY_THRESHOLD && zcr < ZCR_THRESHOLD) {
            silence_frames = 0; // 重置静音帧计数
            if (in_speech) {
                silence_frames++;
                if (silence_frames >= max_silence_frames) {
                    in_speech = 0;
                    //HET_LOGI("find: silence=[%d/%d]\n", silence_frames,max_silence_frames);
                    silence_frames = 0;
                    return 1; // 通讯结束
                }
            }
        } else {
            if (!in_speech) {
                in_speech = 1;
            }
        }
        //HET_LOGI("find: speech=%d silence=[%d/%d]\n", in_speech, silence_frames,max_silence_frames);
    }
    return 0; // 通讯未结束
}

double calculate_energy(const int16_t *audio_data, size_t frame_size, int16_t channel_num) {
    
    double energy = 0.0;
    for (size_t i = 0; i < frame_size/channel_num; i++) {
        energy += audio_data[i* channel_num] * audio_data[i* channel_num];
    }
    return energy / (frame_size / channel_num);
}

double calculate_zcr(const int16_t *audio_data, size_t frame_size, int16_t channel_num) {
    double zcr = 0.0;
    for (size_t i = 1; i < frame_size/channel_num; i++) {
        if ((audio_data[i * channel_num - channel_num] >= 0 && audio_data[i * channel_num ] < 0) || (audio_data[i * channel_num  - channel_num] < 0 && audio_data[i * channel_num ] >= 0)) {
            zcr++;
        }
    }
    return zcr;
}

#if 0
int main() {
    // 示例PCM数据（假设是16位、16kHz采样率）
    int16_t audio_data[] = { /* 你的PCM数据 */ };
    size_t audio_length = sizeof(audio_data) / sizeof(audio_data[0]);

    int result = split_audio(audio_data, audio_length);
    printf("Communication ended: %d\n", result);

    return 0;
}
#endif